<template>
  <div>
    <div class="table-container">
      <el-table
        ref="table"
        v-loading="loading"
        :border="border"
        :height="height"
        style="width: 100%"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          v-if="orderNum"
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column
          v-if="checkBox"
          type="selection"
          width="55"
        ></el-table-column>

        <el-table-column
          v-for="item in tableColumn"
          :key="item.label"
          align="center"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :fixed="item.fixed"
          :sortable="item.sortable"
          :formatter="item.formatter"
        >
          <!-- 自定义头部 -->
          <template v-if="item.slotHeader" #header="scope">
            <slot
              :name="item.slotHeader"
              :data="scope.row"
              :index="scope.$index"
            ></slot>
          </template>

          <!-- 自定义列内容 -->
          <template v-if="item.slotContent" #default="scope">
            <slot
              :name="item.slotContent"
              :data="scope.row"
              :index="scope.$index"
            ></slot>
          </template>
        </el-table-column>
        <el-table-column
          v-if="btnOption.length > 0"
          label="操作"
          :width="btnWidth"
          align="center"
          fixed="right"
        >
          <template #default="{ row, $index }">
            <div class="flex-center">
              <template
                v-for="item in handleBtnOptions(
                  row,
                  btnOption,
                  btnProp || 'status'
                )"
              >
                <el-button
                  v-if="item.show"
                  :key="item.text"
                  plain
                  :type="item.type"
                  :size="item.size || small"
                  :icon="item.icon || ''"
                  @click="handleClick(item.func, row, $index)"
                >
                  {{ item.text }}
                </el-button>
              </template>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <HHpage
      :total="total"
      :background="true"
      :current-page="currentPage"
      @prev-click="prevClick"
      @next-click="nextClick"
      @current-change="currentChange"
    ></HHpage>
  </div>
</template>

<script>
  import HHpage from '@/components/HH-page'

  export default {
    components: { HHpage },
    props: {
      // 是否打开序号
      orderNum: {
        type: Boolean,
        default: false,
      },
      checkBox: {
        type: Boolean,
        default: false,
      },
      // table数据
      tableData: {
        type: Array,
        default() {
          return []
        },
        require: true,
      },
      // table属性
      tableColumn: {
        type: Array,
        default() {
          return []
        },
        require: true,
      },
      btnWidth: {
        type: [String, Number],
        default: '250',
      },
      // 按钮配置
      btnOption: {
        type: Array,
        default() {
          return []
        },
      },
      // 是否要边框
      border: Boolean,
      //  设置表格高度，设置后头部固定
      height: {
        type: Number,
        default: 650,
      },
      total: {
        type: Number,
        default: 50,
      },
      // 以某个属性值为过滤按钮标准
      btnProp: {
        type: String,
        default: '',
      },
    },
    emits: ['page-click', 'selection-change'],
    data() {
      return {
        loading: false,
        currentPage: 1,
      }
    },
    methods: {
      handleClick(func, data, index) {
        if (!func) return
        func(data, index)
      },
      // 过滤按钮
      handleBtnOptions(data, arr, props) {
        return arr.filter((item) => {
          if (item.except instanceof Array) {
            return item.except.includes(data[props])
          }
          return item.except !== parseInt(data[props])
        })
      },
      prevClick(pager) {
        this.$emit('page-click', pager)
      },
      nextClick(pager) {
        this.$emit('page-click', pager)
      },
      currentChange(pager) {
        this.$emit('page-click', pager)
      },
      // 多选框选中
      handleSelectionChange(val) {
        this.$emit('selection-change', val)
      },
      // 重置分页为第一页
      resetPageNum() {
        this.currentPage = 1
      },
      // 重新计算表格
      doLayout() {
        this.$refs.table.doLayout()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .table-container {
    margin-top: 20px;
  }
  .flex-center {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
  }
  ::v-deep {
    .el-button {
      margin-bottom: 10px;
      // &:nth-of-type(2n - 1) {
      //   margin-left: 0;
      // }
    }
  }
</style>
